/*
 * SqQueue.c
 *
 *  Created on: 2011-12-2
 *      Author: tracl01
 */

#include <stdlib.h>
#include <stdio.h>
#define MAXQSIZE	100		//#行的语句末尾都不能加上；

typedef int Elemtype;
typedef struct{
	Elemtype *base;
	int front;
	int rear;
}SqQueue;

int initSqQueue(SqQueue *Q){
	Q->base = (Elemtype *)malloc(MAXQSIZE*sizeof(Elemtype));
	if (!Q->base) {
		exit(EXIT_FAILURE);
	}
	Q->front = Q->rear = 0;
	return EXIT_SUCCESS;
}

int enSqQueue(SqQueue *Q, Elemtype e){
	if ((Q->rear+1)%MAXQSIZE == Q->front) {		//约定rear指向队尾元素的下一个位置；判断队满时
		return EXIT_FAILURE;					//实际上还有空余空间，就不用了；
	}
	Q->base[Q->rear] = e;
	Q->rear = (Q->rear+1)%MAXQSIZE;
	return EXIT_SUCCESS;
}

int deSqQueue(SqQueue *Q, Elemtype *e){
	if (Q->front == Q->rear) {
		return EXIT_FAILURE;
	} else {
		*e = Q->base[Q->front];
		Q->front = (Q->front+1)%MAXQSIZE;
		return EXIT_SUCCESS;
	}
}
/*
int main(void){
	SqQueue Q;Elemtype e;
	if (!initSqQueue(&Q)) {
		printf("init successful\n");
	}
	int i;
	for (i = 0; i < 10; ++i) {
		enSqQueue(&Q, i);
	}
	for (i = 0; i < 10; ++i) {
		deSqQueue(&Q, &e);
		printf("%d ", e);
	}
	return EXIT_SUCCESS;
}
*/
